package com.compton.UI
{
	import com.compton.VO.WineVO;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import libs.FavoritesPanel;
	import libs.MainMask;
	import libs.SliderAssets; 
	
	public class Favorites extends FavoritesPanel
	{
		private var _wineArray:Array = [];
		private var _thumb:Thumbnails;
		private var _lb:LayoutBox;
		private var _layoutBoxSlider:SliderAssets;
		private var _slider:SliderManager;
		private var _layout:Number = 0;
		private var _thumbArray:Array = [];
		private var _lbMask:MainMask;
		
		public function Favorites()
		{
			super();
		}
		
		//Assembles the layoutbox, mask and slider bar everytime the panel recieves information
		private function assemble():void
		{	
			_lb = new LayoutBox(0,false);
			_lb.x = 15;
			_lb.y = 60;
			this.addChild(_lb);
			
			_lbMask = new MainMask();
			_lb.mask = _lbMask;
			_lbMask.x = 15;
			_lbMask.y = 60;
			
			this.Background.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
		}
		
		//allows the window to be clicked and moved
		private function onDown(evt:MouseEvent):void
		{
			stage.nativeWindow.startMove();
		}
		
		//Builds the slider and allows the slider to produce a percentage and move the laybox according to the ammount of percent
		private function addSlider():void
		{	
			_layoutBoxSlider = new SliderAssets();
			_layoutBoxSlider.x = 413;
			_layoutBoxSlider.y = 75;
			_layoutBoxSlider.rotation = 90;
			this.addChild(_layoutBoxSlider);
			
			_slider = new SliderManager();
			_slider.setUpAsstes(_layoutBoxSlider.track, _layoutBoxSlider.handle);
			_layoutBoxSlider.handle.buttonMode = true;
			_slider.addEventListener(Event.CHANGE, onChange);
			
			if(_thumbArray.length < 5)
			{
				this.removeChild(_layoutBoxSlider);	
			}
		}
		
		//produces the percentage amount to move the Layoutboxs Y value accordingly
		private function onChange(evt:Event):void
		{
			_layout = (_lb.height - _lbMask.height) * _slider.percent - (_lbMask.y);
			_lb.y = -_layout;
		}
		
		//Loads the thumbs and starts building the layoutbox to place them into.
		private function loadThumbs():void
		{
			assemble();
			for each(var thumb:WineVO in _wineArray)
			{
				_thumb = new Thumbnails();
				_thumb.imagesVO = thumb;
				_lb.addChild(_thumb);
				_thumbArray.push(_thumb);	
			}
			addSlider();
		}
		
		/**
		 * This getter setter will provide all the wine information that was parsed out from the wineservices class 
		 * @return returns the wineArray full of information on all the specific wines called
		 * 
		 */		
		
		//getter and setter for the wine array to be passed in and out.
		public function get wineArray():Array
		{
			return _wineArray;
		}
		
		/**
		 * @private
		 */		

		//when the search is initiated again the layout box will remove it's children and begin loading thumbs again.
		public function set wineArray(value:Array):void
		{
			_wineArray = value;
			if(_lb)
			{
				while(_lb.numChildren > 0)
				{
					_thumbArray.pop();
					_lb.removeChildAt(0);
				}
			}
			loadThumbs();
		}
	}
}